<?php
/**
 * Created by PhpStorm.
 * User: envision
 * Date: 2018-10-08
 * Time: 12:55
 */

namespace App\Http\Controllers\Api\V1\Manager\User;


use App\Http\Controllers\Controller;
use App\Models\Admin;
use App\Services\Manager\LogService;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Validator;

class LoginController extends Controller
{
    private $namespace = "App\Http\Controllers\Api\V1\Manager\User\LoginController";

    public function login(Request $request)
    {
        $loginType = $request->loginType;
        $phone = $request->phone;
        $code = $request->code;
        $password = $request->password;
        if (!$loginType || !$phone) {
            return $this->apiResponse('', config('errorLiu.NOTCANSHUID'));
        }
        $admin = Admin::select(
            'id',
            'userName',
            'nickname',
            'phone',
            'position',
            'positionDesc',
            'resource',
            'password',
            'status',
            'isSuper'
        )
            ->where('phone', $phone)
            ->where('isDel', 0)
            ->first();
        if (!$admin) {
            return $this->apiResponse('', config('errorLiu.ZHANGHAOBUCUNZAICHOJQINGCHOGXIN'));
        }
        if (!$admin['status']) {
            return $this->apiResponse('', config('errorLiu.ZHANGHAOBUCUNZAICHOJQINGCHOGXIN'));
        }
        if ($loginType == "CODE") {
            if (!$code) {
                return $this->apiResponse('', config('errorLiu.NOTCANSHUID'));
            }
            $smsCode = \RedisDB::get($phone);
            if ($smsCode != $code) {
                return $this->apiResponse('', config('errorLiu.YANZHENGMACUOZIAJIHJDUIYUFNUA'));
            }
            \RedisDB::del($phone);
        } else {
            if (!$password) {
                return $this->apiResponse('', config('errorLiu.NOTCANSHUID'));
            }
            $salt = "liuliuliu" . $admin['id'];
            $password = md5($salt . $password);
            if ($password != $admin['password']) {
                return $this->apiResponse('', config('errorLiu.MIMACUOWUGUANLIYUANDE'));
            }
        }
        unset($admin['password']);
        $accessToken = md5(time() . $admin['id']);
        \RedisDB::del($admin['id'] . $phone);
        \RedisDB::setex($admin['id'] . $phone, 21600, $accessToken);
        Admin::where('id', $admin['id'])->update(['loginTime' => date("Y-m-d H:i:s"), 'loginIp' => $request->getClientIp(), 'accessToken' => $accessToken]);
        $admin['accessToken'] = $accessToken;
        //添加管理员日志开始
        $url = $this->namespace."/login";
        $title = "登录";
        $content = "管理员登录";
        $this->addAdminLog($admin['id'],$admin['userName'],$url,"登录模块",$title,$content,$request->getClientIp());
        //添加管理员日志结束
        return $this->apiResponse($admin);
    }

    public function info(Request $request)
    {
        $adminId = $request->ADMINID;
        $admin = Admin::select(
            'id',
            'userName',
            'nickname',
            'phone',
            'position',
            'positionDesc',
            'resource',
            'status',
            'isSuper'
        )
            ->where('id', $adminId)
            ->where('isDel', 0)
            ->first();
        return $this->apiResponse($admin);
    }

    public function loginOut(Request $request)
    {
        $adminId = $request->ADMINID;
        $phone = $request->ADMINPHONE;
        $userName = $request->ADMINUSERNAME;
        \RedisDB::del($adminId . $phone);
        //添加管理员日志开始
        $url = $this->namespace."/loginOut";
        $title = "退出登录";
        $content = "退出登录";
        $this->addAdminLog($adminId,$userName,$url,"登录模块",$title,$content,$request->getClientIp());
        //添加管理员日志结束
        return $this->apiResponse($adminId);
    }

    /**
     * 忘记修改密码
     * @param Request $request
     * @return string
     */
    public function updatePassWord(Request $request)
    {
        $phone = $request->phone;
        $code = $request->code;
        $password = $request->password;
        if (!$password || !$code || !$phone) {
            return $this->apiResponse('', config('errorLiu.NOTCANSHUID'));
        }
        $smsCode = \RedisDB::get($phone);
        if ($code != $smsCode) {
            return $this->apiResponse('', config('errorLiu.YANZHENGMACUOZIAJIHJDUIYUFNUA'));
        }
        \RedisDB::del($phone);
        $admin = Admin::select(
            'id',
            'userName',
            'nickname',
            'phone',
            'position',
            'positionDesc',
            'resource',
            'password',
            'status',
            'isSuper'
        )
            ->where('phone', $phone)
            ->where('isDel', 0)
            ->first();
        if (!$admin) {
            return $this->apiResponse('', config('errorLiu.ZHANGHAOBUCUNZAICHOJQINGCHOGXIN'));
        }
        $salt = "liuliuliu" . $admin['id'];
        $password = md5($salt . $password);
        $update = [];
        $update['password'] = $password;
        $update['accessToken'] = null;
        $update['updateTime'] = date("Y-m-d H:i:s");
        Admin::where('id', $admin['id'])->where('isDel', 0)->update($update);
        \RedisDB::del($admin['id'] . $phone);
        //添加管理员日志开始
        $url = $this->namespace."/updatePassWord";
        $title = "修改";
        $content = "修改密码";
        $this->addAdminLog($admin['id'],$admin['userName'],$url,"登录模块",$title,$content,$request->getClientIp());
        //添加管理员日志结束
        return $this->apiResponse(true);
    }
}